﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using forum.ApplicationCode;
using forum.Data;

namespace forum
{
    public partial class AdminAddForum : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!Membership.IsAdmin())
                Response.Redirect("forumWelcome.aspx");

            if (!IsPostBack)
            {

                lblNewForumName.Text = "New forum name is: " + (string)Session["addSubForumName"];

                chkbxlstAddSubForumModerators.DataSource = ListManager.GetNewSubForumModerators();
                chkbxlstAddSubForumModerators.DataTextField = "_Text";
                chkbxlstAddSubForumModerators.DataValueField = "_Value";
                chkbxlstAddSubForumModerators.DataBind();

            }

        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            using (forumDataContext data = new forumDataContext())
            {
                //new forum id
                int forumId = data.sp_InsertSubForumWrap((string)Session["addSubForumName"]).FirstOrDefault().Column1.Value;
                foreach (Control c in plModerators.Controls)
                {
                    if (c is CheckBoxList)
                    {
                        foreach (ListItem li in ((CheckBoxList)c).Items)
                        {
                            if (li.Selected == true)
                            {
                                //add moderator that checked by admin
                               data.sp_AddForumModerator(forumId, int.Parse(li.Value),DateTime.Now);

                            }
                        }


                    }

                }
            }

            //clean session and redirect
            Session["addSubForumName"] = null;
            Response.Redirect("forumWelcome.aspx");


        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Session["addSubForumName"] = null;
            Response.Redirect("AdminArea.aspx");
         }
    }
}